/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.component.cim.pusher;

import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.entity.Message;
import com.workcell.workdata.im.exception.ChatBlockedException;
import com.workcell.workdata.im.exception.ChatLimitedException;
import com.workcell.workdata.im.model.app.Origin;
import com.workcell.workdata.im.model.response.ResponseEntity;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

@Component
public class MessagePusherProxy implements ApplicationListener<ApplicationStartedEvent> {

	@Resource
	private ApplicationContext applicationContext;

	@Resource
	private DefaultMessagePusher defaultMessagePusher;

	private final HashMap<String,BaseMessagePusher> pusherMap = new HashMap<>();


	public ResponseEntity<?> push(Message message , Origin origin) {

		BaseMessagePusher pusher = getMessagePusher(message.getAction());

		try {
			pusher.preHandle(message,origin);

			pusher.push(message,origin);

		}catch (ChatBlockedException ignore){
			/*
			 * 当聊天被拉黑，或者群被禁止发言 则抛出ChatBlockedException
			 */
			return ResponseEntity.make(HttpStatus.LOCKED,"被拉黑，或者群被禁止发言");

		}catch (ChatLimitedException ignore){
			/*
			 * 当对好友删除，或者群成员被禁止发言 ChatLimitedException
			 */
			return ResponseEntity.make(HttpStatus.FORBIDDEN,"被删除好友，或者群被禁止发言");
		}

		ResponseEntity<Long> response = new ResponseEntity<>();

		response.setData(message.getId());

		response.setTimestamp(message.getCreateTime().getTime());

		return response;
	}



	private BaseMessagePusher getMessagePusher(String action) {

		BaseMessagePusher messagePusher = pusherMap.get(action);

		return messagePusher == null ? defaultMessagePusher : messagePusher;
	}

	@Override
	public void onApplicationEvent(ApplicationStartedEvent startedEvent) {

		pusherMap.put(MessageAction.ACTION_0,applicationContext.getBean(FriendMessagePusher.class));

		pusherMap.put(MessageAction.ACTION_3, applicationContext.getBean(GroupMessagePusher.class));

		pusherMap.put(MessageAction.ACTION_4, applicationContext.getBean(GroupMessagePusher.class));

		pusherMap.put(MessageAction.ACTION_200, applicationContext.getBean(MicroServerMessagePusher.class));
		pusherMap.put(MessageAction.ACTION_202, applicationContext.getBean(MicroServerMessagePusher.class));
		pusherMap.put(MessageAction.ACTION_203, applicationContext.getBean(MicroServerMessagePusher.class));
		pusherMap.put(MessageAction.ACTION_204, applicationContext.getBean(MicroServerMessagePusher.class));

		pusherMap.put(MessageAction.ACTION_400, applicationContext.getBean(OrganizationMessagePusher.class));
		pusherMap.put(MessageAction.ACTION_401, applicationContext.getBean(OrganizationMessagePusher.class));
		pusherMap.put(MessageAction.ACTION_402, applicationContext.getBean(OrganizationMessagePusher.class));
		pusherMap.put(MessageAction.ACTION_404, applicationContext.getBean(OrganizationMessagePusher.class));
		pusherMap.put(MessageAction.ACTION_405, applicationContext.getBean(OrganizationMessagePusher.class));
		pusherMap.put(MessageAction.ACTION_406, applicationContext.getBean(OrganizationMessagePusher.class));
	}
}
